//
//  SCNInvestmentBackMoneyViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/11/16.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNInvestmentBackMoneyViewController: SCNTableViewController {
    
    fileprivate var endDataArray = Array<SCNInvestmentListModels>()

    override func viewWillAppear(_ animated: Bool) {
        
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = nil
        
        super.viewWillDisappear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        configurationUI()
        
        request()
    }


    // MARK: UI
    
    func configurationUI() {
        // 理财指南
        let lcznImgView = UIImageView(image: UIImage(named: "scn_investment_list_head_icon"))
        lcznImgView.isUserInteractionEnabled = true
        view.addSubview(lcznImgView)
        lcznImgView.snp.makeConstraints { (make) in
            make.leading.top.trailing.equalToSuperview()
            make.height.equalTo(338 * SCNConstants.SCREENWIDTH / 720)
        }
        let lczhBtn = UIButton()
        lczhBtn.backgroundColor = .clear
        lczhBtn.addTarget(self, action: #selector(lcznAction), for: .touchUpInside)
        lcznImgView.addSubview(lczhBtn)
        lczhBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 112, height: 35))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20)
        }
        // 标列表
        self.createTableView(.zero, style: .plain, isRefreshNewData: true, isRefreshMoreData: true) { (table) in
            
            table.rowHeight = 150
            
            table.separatorStyle = .none
            table.estimatedSectionFooterHeight = 0
            table.estimatedSectionHeaderHeight = 0
            table.register(SCNInvestmentCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(SCNInvestmentCell.classForCoder()))
        }
        
        self.tableView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(lcznImgView.snp.bottom)
            
        }
        
    }
    
    // MARK: DATA
    override func request() {
        
        super.request()
        
        let param = [
            "page" : "\(self.pageIndex)",
            "number" : "20",
            "cateid" : "1"
        ]
        
        SCNInvestmentBackMoneyViewModel().requestInvestInvestStatus(param, successBlock: { [unowned self] (dataresult) in
            
            if self.pageIndex == 1 {
                self.endDataArray.removeAll()
            }
            
            if let list = dataresult.list {
                for tmp in list {
                    self.endDataArray.append(tmp)
                }
                self.tableView.reloadData()
            }
            
            self.dataTotal(dataresult.total!)
        })
    }
    
    // MARK: ACTION
    
    func lcznAction() {
        self.scnPushViewController(SCNMoneyGuideViewController())
    }
    
}

extension SCNInvestmentBackMoneyViewController
{
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.endDataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(SCNInvestmentCell.classForCoder()), for: indexPath) as! SCNInvestmentCell
        
        if endDataArray.count > 0 {
            cell.setInvestmentTableViewCell(endDataArray[indexPath.row], index: indexPath, status: 2)
        }
        
        
        return cell
        
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        defer {
            tableView.deselectRow(at: indexPath, animated: true)
        }
        if endDataArray.count > 0 {
            let detailVC = SCNInvestDetailViewController()
            detailVC.title = endDataArray[indexPath.row].title
            detailVC.loansn = endDataArray[indexPath.row].loansn
            self.scnPushViewController(detailVC)
        }
        
    }
    
}
